import { createStore } from 'vuex'

export default createStore({
  state: {
    adminId: localStorage.getItem('adminId') || null,
    adminName: localStorage.getItem('adminName') || null,
    token: localStorage.getItem('adminToken') || null,
    businesses: [],
    users: [],
    coupons: [],
    levels: []
  },
  
  getters: {
    isLoggedIn: (state) => !!state.token,
    adminId: (state) => state.adminId
  },
  
  mutations: {
    setAdminInfo(state, { adminId, adminName, token }) {
      state.adminId = adminId
      state.adminName = adminName
      state.token = token
      localStorage.setItem('adminId', adminId)
      localStorage.setItem('adminName', adminName)
      localStorage.setItem('adminToken', token)
    },
    
    setBusinesses(state, businesses) {
      state.businesses = businesses
    },
    
    setUsers(state, users) {
      state.users = users
    },
    
    setCoupons(state, coupons) {
      state.coupons = coupons
    },
    
    setLevels(state, levels) {
      state.levels = levels
    },
    
    logout(state) {
      state.adminId = null
      state.adminName = null
      state.token = null
      localStorage.removeItem('adminId')
      localStorage.removeItem('adminName')
      localStorage.removeItem('adminToken')
    }
  },
  
  actions: {
    login({ commit }, { adminId, adminName, token }) {
      commit('setAdminInfo', { adminId, adminName, token })
    },
    
    logout({ commit }) {
      commit('logout')
    }
  }
})
